Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: {
      username: '福小州',
      dormitory: '11号楼222',
      college: '计算机与大数据',
      major: '软件工程',
      studentId: '888888888',
      points: 82,
      avatar: '/assets/person/avartar-default.png',
      phone: '13333333333',
      email: '2222222222@qq.com'
    },
    scoreList: [
      {
        count: '1',
        event: '认真完成卫生值日',
        date: '2025.05.12'
      },
      {
        count: '1',
        event: '认真完成卫生值日',
        date: '2025.05.09'
      },
      {
        count: '1',
        event: '认真完成卫生值日',
        date: '2025.05.07'
      },
      {
        count: '-1',
        event: '未认真完成卫生值日',
        date: '2025.05.05'
      },
      {
        count: '1',
        event: '帮舍友带快递',
        date: '2025.05.03'
      },
      {
        count: '-1',
        event: '未认真完成卫生值日',
        date: '2025.05.02'
      },
      {
        count: '1',
        event: '帮舍友带快递',
        date: '2025.05.02'
      },
      {
        count: '-1',
        event: '未认真完成卫生值日',
        date: '2025.05.01'
      },
      {
        count: '-1',
        event: '未认真完成卫生值日',
        date: '2025.05.01'
      }
    ],
    clicked: false
  },
  getTokenData() {
    try {
      const tokenData = wx.getStorageSync('auth_token');
      if (tokenData && Date.now() < tokenData.expireTime) {
        return tokenData;
      } else {
        return null;
      }
    } catch (e) {
      console.error('获取 Token 失败', e);
      return null;
    }
  },
  getRequestHeader() {
    const tokenData = this.getTokenData();
    const headers = {
      'Content-Type': 'application/json'
    };
    if (tokenData) {
      headers[tokenData.tokenName] = tokenData.tokenValue;
      console.log('请求头:', headers);
    }
    return headers;
  },
  getUserInfo(){
    const header = this.getRequestHeader();
    wx.request({
      url: 'https://ufhmfcbjuwjt.sealoshzh.site/user/info',
      method: 'GET',
      header: header,
      data: {},
      success:(res) => {
        console.log('个人信息数据:',res);
        this.setData({
          details: res.data
        })
      },
      fail: (err) => {
        console.error('报错:',err);
      }
    }) 
  },
  editInfo(){
    this.setData({
      clicked: true
    })
  },
  returnIfo(){
    this.setData({
      clicked: false
    })
  },
  onNameInput(e) {
    this.setData({
      'details.username': e.detail.value,
    })
  },
  onDormitoryInput(e){
    this.setData({
      'details.dormitory': e.detail.value,
    })
  },
  onCollegeInput(e){
    this.setData({
      'details.college': e.detail.value,
    })
  },
  onMajorInput(e){
    this.setData({
      'details.major': e.detail.value,
    })
  },
  onStudentIdInput(e){
    this.setData({
      'details.studentId': e.detail.value
    })
  },
  onPhoneInput(e){
    this.setData({
      'details.phone': e.detail.value
    })
  },
  onEmailInput(e){
    this.setData({
      'details.email': e.detail.value
    })
  },
  saveEdit(){
    const header = this.getRequestHeader();
    wx.request({
      url: 'https://ufhmfcbjuwjt.sealoshzh.site/user/update',
      method: 'PUT',
      header: header,
      data: this.data.details,
      success:(res) => {
        console.log('更改个人信息数据成功');
      },
      fail: (err) => {
        console.error('报错:',err);
      }
    })
  },
  uploadAvatar() {
    wx.chooseImage({
      success: (res) => { 
        const tempFilePaths = res.tempFilePaths;
        this.uploadFileToServer(tempFilePaths[0]); 
      },
      fail: (err) => {
        console.error('选择图片失败:', err);
      }
    });
  },
  uploadFileToServer(tempFilePath) {
    const header = this.getRequestHeader();
    wx.uploadFile({
      url: 'https://ufhmfcbjuwjt.sealoshzh.site/user/uploadAvatar', 
      filePath: tempFilePath,
      name: 'file', 
      header: header,
      success: (res) => {
        if (res.statusCode === 200) {
          const result = JSON.parse(res.data);
          console.log('头像上传成功:', result);
        } else {
          console.error('上传失败，状态码:', res.statusCode);
        }
      },
      fail: (err) => {
        console.error('上传失败:', err);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})